package com.wx.cloud.user.controller;

import com.wx.cloud.user.common.CommonResult;
import com.wx.cloud.user.model.Page;
import com.wx.cloud.user.model.User;
import com.wx.cloud.user.service.UserService;
import com.wx.cloud.user.utils.MyDate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Api(tags="用户管理")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("用户信息完善")
    @PostMapping("/perfect")
    public CommonResult perfect(@RequestBody User user){
        HashMap<String, Integer> data = new HashMap<>();
        int result = userService.perfect(user);
        data.put("data",result);
        return CommonResult.success(data);
    }

    @ApiOperation("根据用户编号获取单个用户")
    @GetMapping("/findUserByUserCode")
    public CommonResult findUserByUserCode(@RequestParam String userCode){
        HashMap<String, Object> data = new HashMap<>();
        User user = userService.findUserByUserCode(userCode);
        data.put("data",user);
        return CommonResult.success(data);
    }

    @ApiOperation("用户列表")
    @GetMapping("/getUserList")
    public CommonResult getUserList(@RequestParam Map<String, Object> data){
        HashMap<String, Object> map = new HashMap<>();
        List<User> users = userService.getUserList(data);
        int count = userService.getSelectCount(data);
        Page page = new Page();
        page.setList(users);
        page.setTotal(count);
        map.put("list",users);
        map.put("count",count);
        return CommonResult.success(map);
    }

    @ApiOperation("修改用户信息")
    @PostMapping("/updateUser")
    public CommonResult updateUser(@RequestParam Map<String, Object> data){
        HashMap<String, Object> map = new HashMap<>();
        int result = userService.updateUser(data);
        map.put("data",result);
        return CommonResult.success(map);
    }

    @ApiOperation("删除用户")
    @PostMapping("/delUser")
    public CommonResult delUser(@RequestBody User user){
        HashMap<String, Object> data = new HashMap<>();
        int result = userService.delUser(user);
        data.put("data",result);
        return CommonResult.success(data);
    }

    @ApiOperation("修改用户状态")
    @PostMapping("/updateState")
    public CommonResult updateState(@RequestBody User user){
        HashMap<String, Object> data = new HashMap<>();
        int result = userService.updateState(user);
        data.put("data",result);
        return CommonResult.success(data);
    }

    @GetMapping("/checkAccessToUri")
    public CommonResult checkAccessToUri(@RequestParam String uri, @RequestParam String username){
        return userService.checkAccessToUri(uri,username);
    }

}
